package com.example.asa;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/resources")
public class ResourcesServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 直接转发到发布预警信息页面
        RequestDispatcher dispatcher = request.getRequestDispatcher("/alerts.jsp");
        dispatcher.forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        // 获取表单数据
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String severity = request.getParameter("severity");
        String publisher = request.getParameter("publisher"); // ✅ 用户可修改的发布人字段

        // 验证输入数据
        if (title == null || title.isEmpty() ||
                content == null || content.isEmpty() ||
                severity == null || severity.isEmpty() ||
                publisher == null || publisher.isEmpty()) {
            request.setAttribute("message", "标题、内容、等级和发布人都不能为空");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/alerts.jsp");
            dispatcher.forward(request, response);
            return;
        }

        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = DatabaseUtil.getConnection();
            if (conn == null) {
                System.err.println("Error: 数据库连接为空");
                request.setAttribute("message", "数据库连接失败");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/alerts.jsp");
                dispatcher.forward(request, response);
                return;
            }

            // 插入到 alerts 表
            String sql = "INSERT INTO alerts (title, content, severity, publish_time, publisher) VALUES (?, ?, ?, NOW(), ?)";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, title);
            stmt.setString(2, content);
            stmt.setString(3, severity);
            stmt.setString(4, publisher); // ✅ 使用用户提交的发布人

            int rowsInserted = stmt.executeUpdate();
            if (rowsInserted > 0) {
                request.setAttribute("message", "预警信息发布成功!");
            } else {
                request.setAttribute("message", "预警信息发布失败，请重试");
            }

        } catch (SQLException e) {
            System.err.println("Error inserting alert: " + e.getMessage());
            e.printStackTrace();
            request.setAttribute("message", "发布预警信息时发生错误");
        } finally {
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        // 返回同一页面并显示提示信息
        RequestDispatcher dispatcher = request.getRequestDispatcher("/disasterManage.jsp");
        dispatcher.forward(request, response);
    }
}
